package cn.njyd.corejava.thread.myday01;

class Ticket{
    private int num;
    private String start;
    private String end;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getStart() {
        return start;
    }

    public void setStart(String start) {
        this.start = start;
    }

    public String getEnd() {
        return end;
    }

    public void setEnd(String end) {
        this.end = end;
    }

    public Ticket() {
    }

    public Ticket(int num, String start, String end) {
        this.num = num;
        this.start = start;
        this.end = end;
    }

    @Override
    public String toString() {
        return "Ticket{" +
                "num=" + num +
                ", start='" + start + '\'' +
                ", end='" + end + '\'' +
                '}';
    }
}

class TicketThread extends Thread{
    private Ticket ticket;

    public TicketThread(Ticket ticket){
        this.ticket=ticket;
    }

    public TicketThread(Ticket ticket,String name){
        super(name);
        this.ticket=ticket;
    }

    @Override
    public void run() {
        while(true){
            if(ticket.getNum()>0){
                ticket.setNum(ticket.getNum()-1);
                System.out.println(this.getName()+"卖出一张，剩余["+ticket.getNum()+"]");
            }else{
                System.out.println("票已经卖完");
                return;
            }
        }
    }
}

public class MyThread01 {
    public static void main(String[] args) {
        Ticket ticket=new Ticket(50,"nanjing","shanghai");
        TicketThread t1=new TicketThread(ticket,"南京南站");
        TicketThread t2=new TicketThread(ticket,"南京小红山");
        TicketThread t3=new TicketThread(ticket,"南京站");
        //启动线程,不能直接调用线程的run方法来启动线程
        t1.start();//该方法也只能被调用一次
        t2.start();
        t3.start();

    }



}
